#!/bin/bash
# Jon Peeters - June 2011

# tarlinappux is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

build_new_version() {
	f="$1"
	cp -f installux.sh "$f/install.sh"
	lastmodsec=$(find "$f" -printf '%T@\n' | sort -r |head -1)  # In order to have :		
	lastmod=$(date --utc --date "1970-01-01 $lastmodsec sec" "+%y%m%d-%H%M") # a version equals to last modification
	echo "$lastmod" > "$f/version"
	if [[ -z "$password" ]]; then
		echo -n "WARNING : don't forget to remove files on $projectname before the upload ! [Ok]"
		read 
		echo -en "\n$projectname password : "
		read password
		clear
		echo -e "Building new version of...\n_____________________________\n"
		end=""	
	fi
	echo "* $f"		
	tar -czf "$f.tar.gz" "$f"
	python googlecode_upload.py -s "$f" -p "$projectname" -u "$user" -w "$password" "$f.tar.gz"
}

projectname="linappux"
user="linappux"
end="Nothing changed => no build needed"
clear
 
for f in $(ls); do
	if [[ -d "$f" ]]; then
		lastmodsec=$(find "$f" -printf '%T@\n' | sort -r |head -1)			
		lastmod=$(date --utc --date "1970-01-01 $lastmodsec sec" "+%y%m%d-%H%M")
		lastversion=$(cat "$f/version" 2>/dev/null |egrep -o "^[0-9]{6}-[0-9]{4}$")
		if [[ -z "$lastversion" ]] || [[ "$lastversion" < "$lastmod" ]]; then				
			if [[ -f "$HOME/.linappux_pgms" ]]; then
				testdir="$(cat $HOME/.linappux_pgms)/$f"
				if [[ -d "$testdir" ]]; then
					lastcopysec=$(find "$testdir" -printf '%T@\n' | sort -r |head -1)			
					lastcopy=$(date --utc --date "1970-01-01 $lastcopysec sec" "+%y%m%d-%H%M")
					if [[ "$lastmod" > "$lastcopy" ]]; then # Before building a new version, sources will be copied for testing
						echo -ne "Installing $f in $testdir...\t"					
						cp -rf "$f"/* "$testdir/"
						if [[ $? -eq 0 ]]; then
							echo "Ok"
							exit 0
						else
							echo "Error"
							exit 1
						fi
					else
						build_new_version "$f"
					fi
				else
					clear
					echo "! $testdir is not yet installed !"
					echo -ne "Installing $f in $testdir...\t"
					mkdir "$testdir"
					if [[ $? -ne 0 ]]; then
						echo "ERROR : cannot create $testdir"
						exit 1
					fi					
					cp -rf "$f"/* "$testdir/"
					if [[ $? -eq 0 ]]; then
						echo "Ok"
						exit 0
					else
						echo "Error"
						exit 1
					fi
					end= ""
				fi
			else
				echo "! No directory defined for linappux programs !"
			fi
		fi
	else
		continue
	fi
done

echo -e "$end\n_____________________________\n\n-- Mercurial --\n"
hg commit -m ' '
hg push
rm version 2>/dev/null
